package com.my12306.user.service.auth.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my12306.common.enums.CodeEnum;
import com.my12306.common.util.PageUtils;
import com.my12306.common.util.SnowflakeIdWorker;
import com.my12306.common.util.response.ResponseData;
import com.my12306.user.dao.auth.RolePermissionDao;
import com.my12306.user.model.po.auth.RolePermissionEntity;
import com.my12306.user.service.auth.RolePermissionService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service("rolePermissionService")
public class RolePermissionServiceImpl extends ServiceImpl<RolePermissionDao, RolePermissionEntity> implements RolePermissionService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        return new PageUtils();
    }

    @Override
    @Transactional
    public ResponseData deleteRolePermissionByRoleIds(List<Long> ids) {
        if (this.baseMapper.deleteBatchRolePermByRoleIds(ids)) {
            return ResponseData.ok();
        }
        return ResponseData.fail(CodeEnum.DELETE_FAIL.getCode(), CodeEnum.DELETE_FAIL.getMessage());
    }

    @Override
    public List<Long> queryPermIdListByRoleId(Long roleId) {
        return this.baseMapper.selectList(new QueryWrapper<RolePermissionEntity>().eq("role_id", roleId)).stream().parallel().map(x -> {
            return x.getPermId();
        }).collect(Collectors.toList());
    }

    @Override
    public Boolean saveBatchRolePerms(List<RolePermissionEntity> permissionEntityList) {
        if (permissionEntityList != null && permissionEntityList.size() > 0) {
            permissionEntityList = permissionEntityList.parallelStream().map(x -> {
                x.setId(SnowflakeIdWorker.generateId());
                return x;
            }).collect(Collectors.toList());
            return this.baseMapper.savaBatchRolePerms(permissionEntityList);
        }
        return false;
    }

    @Override
    public void deleteRolePermByPermIds(List<Long> allIds) {
        this.baseMapper.deleteBatchRolePermByPermIds(allIds);
    }

}
